package cn.lastcrdg.testslide;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by zhang on 2017/10/17.
 */

public class UpLoaderActivity extends Activity {

    private static final String TAG = UpLoaderActivity.class.getSimpleName();
    private RecyclerView mRecyclerView;
    private RefreshAdapter adapter;
    List<String[]> list = new ArrayList<>();//初始数据集合
    List<String[]> modList = new ArrayList<>();//修改后的数据集合
    private boolean isTwoData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uploader_activity);
        initView();
    }
    private void initView(){
        modifyData(initData());
        mRecyclerView = (RecyclerView) findViewById(R.id.uploader);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RefreshAdapter(this, modList, isTwoData);
        adapter.setFootViewText("数据加载中.....");
        mRecyclerView.setAdapter(adapter);
    }

    //将两条数据改为一条数据，关键
    private void modifyData(List<String[]> list) {
        int count = 0; // 修改数据之后的总条数
        if (list.size() % 2 == 0) {
            count = list.size() / 2;
        } else {
            count = list.size() / 2 + 1;
        }
        for (int i = 0; i < count; i++) {
            if (i * 2 < list.size()) {
                isTwoData = false;
                String[] data1 = list.get(i * 2);
                List<String> list1 = new ArrayList<>();
                List<String> strings1 = Arrays.asList(data1);
                list1.addAll(strings1);
                if (i * 2 + 1 < list.size()) {
                    isTwoData = true;
                    String[] data2 = list.get(i * 2 + 1);
                    List<String> strings2 = Arrays.asList(data2);
                    list1.addAll(strings2);
                }
                //这里我直接将数组转化为集合后合并，因为的测试数据都是一样的，其实应该重新写一个Bean对象
                String[] strings = new String[list1.size()];
                String[] array = list1.toArray(strings);
                Log.d(TAG, "modifyData: " + list1.toString());
                modList.add(array);
            }
        }
    }

    //初始化获取两条数据
    private List<String[]> initData() {
        String[] datas1 = {"第一条", "ew", "re", "tr", "yt", "uy"};
        String[] datas2 = {"第二条", "23", "cd", "ew", "de", "as"};
        String[] datas3 = {"第三条", "rt", "gfb", "dfgr", "de", "41"};
        String[] datas4 = {"第四条", "ht", "vf", "4r", "gdf", "dfgdfh"};
        String[] datas5 = {"第五条", "fg", "hy", "fr", "vv", "ss"};
        String[] datas6 = {"第六条", "fd", "5112", "wew", "trt4", "56"};
        String[] datas7 = {"第七条", "d", "00", "02", "46", "79"};
        list.add(datas1);
        list.add(datas2);
        list.add(datas3);
        list.add(datas4);
        list.add(datas5);
        list.add(datas6);
        list.add(datas7);
        return list;
    }
}
